package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.TbGoodsCount;
import com.ruoyi.business.domain.TbGoodsList;
import com.ruoyi.business.mapper.TbGoodsListMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TbGoodsOrderMapper;
import com.ruoyi.business.domain.TbGoodsOrder;
import com.ruoyi.business.service.ITbGoodsOrderService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-24
 */
@Service
public class TbGoodsOrderServiceImpl implements ITbGoodsOrderService 
{
    @Autowired
    private TbGoodsOrderMapper tbGoodsOrderMapper;

    @Autowired
    private TbGoodsListMapper tbGoodsListMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param ID 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public TbGoodsOrder selectTbGoodsOrderById(Long ID)
    {
        return tbGoodsOrderMapper.selectTbGoodsOrderById(ID);
    }

    @Override
    public TbGoodsOrder selectTbGoodsOrderByGoodsId(Long goodsListId) {
        return tbGoodsOrderMapper.selectTbGoodsOrderByGoodsId(goodsListId);
    }

    @Override
    public List<String> selectTbGoodsAccountMonth() {
        return tbGoodsOrderMapper.selectTbGoodsAccountMonth();
    }

    @Override
    public List<TbGoodsCount> selectTbGoodsCountList(TbGoodsCount tbGoodsCount) {
        List<TbGoodsCount> list = tbGoodsOrderMapper.selectTbGoodsCountList(tbGoodsCount);
        return list;
    }

    @Override
    public List<TbGoodsCount> selectTbGoodsCount(TbGoodsCount tbGoodsCount) {
        return tbGoodsOrderMapper.selectTbGoodsCount(tbGoodsCount);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tbGoodsOrder 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TbGoodsOrder> selectTbGoodsOrderList(TbGoodsOrder tbGoodsOrder)
    {
        return tbGoodsOrderMapper.selectTbGoodsOrderList(tbGoodsOrder);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tbGoodsOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbGoodsOrder(TbGoodsOrder tbGoodsOrder)
    {
        if (tbGoodsOrder.getGoodsId() == null) {
            return tbGoodsOrderMapper.insertTbGoodsOrder(tbGoodsOrder);
        }
        TbGoodsOrder list = tbGoodsOrderMapper.selectTbGoodsOrderByGoodsId(tbGoodsOrder.getGoodsId());
        long mount = list.getMount() - tbGoodsOrder.getMount();
        if(mount < 0 ){
            throw new RuntimeException("库存不足");
        }
        //修改库存
        TbGoodsList tbGoodsList = new TbGoodsList();
        tbGoodsList.setGoodsId(tbGoodsOrder.getGoodsId());
        tbGoodsList.setGoodsMount(mount);
        tbGoodsListMapper.updateTbGoodsListByGoodsId(tbGoodsList);
        //生成订单
        tbGoodsOrder.setCreateTime(DateUtils.getNowDate());
        return tbGoodsOrderMapper.insertTbGoodsOrder(tbGoodsOrder);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tbGoodsOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTbGoodsOrder(TbGoodsOrder tbGoodsOrder)
    {
        tbGoodsOrder.setUpdateTime(DateUtils.getNowDate());
        return tbGoodsOrderMapper.updateTbGoodsOrder(tbGoodsOrder);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTbGoodsOrderByIds(String ids)
    {
        return tbGoodsOrderMapper.deleteTbGoodsOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param ID 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTbGoodsOrderById(Long ID)
    {
        return tbGoodsOrderMapper.deleteTbGoodsOrderById(ID);
    }
}
